import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @author Jason
 * @PACKAGE_NAME PACKAGE_NAME
 * @Description jdbc练习
 * @date 2023-12-05 21:55
 */
public class Test {
    private static String driver = "com.mysql.cj.jdbc.Driver";
    /*用户名*/
    private static String username = "root";
    /*密码*/
    private static String password = "123456";
    /*数据库名称*/
    private static String database = "xing";
    /*URL格式：jdbc:mysql://IP:端口/数据库名称/？额外参数*/
    private static String url = "jdbc:mysql://localhost:3306/" + database + "?serverTimezone=UTC";


    public static void main(String[] args) throws SQLException {
        Connection connection = null;
        try {
            /*加载数据库驱动*/
            Class.forName(driver);
            System.out.println("数据库驱动加载成功!");
            connection = DriverManager.getConnection(url, username, password);
            System.out.println("数据库连接成功!");
        } catch (ClassNotFoundException e) {
            System.out.println("数据库驱动加载失败");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("数据库连接失败");
            e.printStackTrace();
        }finally {
            /*释放数据库连接*/
            connection.close();
            System.out.println("数据库关闭成功!");
        }
    }
}

